$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/recharge/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true },
            { label: '操作', name: 'status', width: 100, formatter: function(value, options, row){
                    var rowId=options.rowId;
                    if(value == 1){
                        return  '<span style="color: #0d6aad" onclick=updateStatusOpen('+rowId+','+value+');>审核</span>' +
                            '<span style="color: #0d6aad" onclick=copyTxId('+rowId+');>  复制tx_id</span>' +
                            '<span style="color: #0d6aad" onclick=showImg('+rowId+');>  查看凭证</span>';;
                    }else {
                        return  '<span style="color: #0d6aad" onclick=updateStatusOpen('+rowId+','+value+'); >查看</span>';
                    }
                }},
			{ label: '状态', name: 'status', index: 'status', width: 60, formatter: function(value, options, row){
                    if(value === 1){
                        return  '待审核';
                    }else if(value === 3){
                        return  '审核通过';
                    }else{
                        return "审核拒绝";
                    }
                }},
			{ label: '充值用户ID', name: 'userId', index: 'user_id', width: 60 },
			{ label: '充值币种', name: 'symbol', index: 'symbol', width: 60 },
			{ label: '充值金额', name: 'amount', index: 'amount', width: 60 },
			{ label: 'TXID', name: 'txId', index: 'tx_id', width: 60 },
            { label: '充值凭证', name: 'evidence', index: 'evidence', width: 30 },
			{ label: '审核失败原因', name: 'reason', index: 'reason', width: 100,hidden:true },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 100 },
			{ label: '审核时间', name: 'updateTime', index: 'update_time', width: 100 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            userId:null,
            mobile:null,
            status:null,
            createTime:null,
            createTime1:null
        },
		showList: true,
		title: null,
		recharge: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.recharge = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = "sys/recharge/updateStatus";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.recharge),
                    success: function(r){
                        if(r.code === 0){
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        reset:function(){
            vm.q = {userId:null,mobile:null, status:null,createTime:null,createTime1:null};
            $("#combo").val('');
            $('table td input').val('');
            vm.reload();
        },
		getInfo: function(id){
			$.get(baseURL + "sys/recharge/info/"+id, function(r){
                vm.recharge = r.recharge;
            });
		},
		reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            var createTime =   $("#createTime").val();
            var createTime1 = $("#createTime1").val();
            if (createTime.length > 0 && createTime1.length > 0) {
                var startTmp = createTime.split("-");
                var endTmp = createTime1.split("-");
                var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                if (sd.getTime() > ed.getTime()) {
                    alert("开始日期不能大于结束日期");
                    return ;
                }
            }
            if(createTime !=null && createTime != ''){
                createTime += " 00:00:00";
            }
            if(createTime1 !=null && createTime1 != ''){
                createTime1 += " 23:59:59";
            }
            parmas=	{'userId':vm.q.userId,'mobile':vm.q.mobile,'status':vm.q.status,'mobile':vm.q.mobile,'createTime':vm.q.createTime,'createTime1':vm.q.createTime1};
            $("#jqGrid").jqGrid('setGridParam',{
                postData:parmas,
                page:1
            }).trigger("reloadGrid");
        },
        exportExcel:function(){
            var createTime =   $("#createTime").val();
            var createTime1 = $("#createTime1").val();
            if (createTime.length > 0 && createTime1.length > 0) {
                var startTmp = createTime.split("-");
                var endTmp = createTime1.split("-");
                var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                if (sd.getTime() > ed.getTime()) {
                    alert("开始日期不能大于结束日期");
                    return ;
                }
            }
            if(createTime !=null && createTime != ''){
                createTime += " 00:00:00";
            }
            if(createTime1 !=null && createTime1 != ''){
                createTime1 += " 23:59:59";
            }
            vm.q = {status:vm.q.statuas,userId:vm.q.userId,mobile:vm.q.mobile,createTime:createTime,createTime1:createTime1};
            var url = "sys/Recharge/export";
            var f = document.createElement("form");
            document.body.appendChild(f);
            var input = document.createElement("input");
            input.type = "hidden";
            f.appendChild(input);
            input.name = "param";
            input.value = JSON.stringify(vm.q);
            f.method="POST";
            f.action = baseURL + url;
            f.submit();
            f.remove();
        }
	}
});
var rechargeId;
function updateStatusOpen(rowId,status) {
    if(status==1){
        $("#btn_no").show();
        $("#btn_ok").show();
        $("#btn_remove").hide();
    }else if(status==3){
        $("#btn_no").hide();
        $("#btn_ok").hide();
    }else{
        $("#btn_no").hide();
        $("#btn_ok").hide();
        $("#btn_remove").show();
    }
    // $("#failCauseDiv").hide();
    var rowData = jQuery("#jqGrid").jqGrid("getRowData",rowId);
    rechargeId = rowData.id;
    $("#rechargeId").html(rowData.id);
    $("#userId").html(rowData.userId);
    $("#parentId").html(rowData.parentId);
    $("#parentName").html(rowData.parentName);
    $("#parentMobile").html(rowData.parentMobile);
    $("#symbol").html(rowData.symbol);
    $("#amount").html(rowData.amount);
    $("#payType").html(payTypeSwitch(rowData.payType));
    $("#payTime").val(rowData.createTime);
    $("#payVoucher").html(rowData.txId);
    $("#reason").html('');
    if(status==3){
        $("#failCauseDiv").show();
        $("#reason").html(rowData.reason);
    }
    $('#myModal').modal();
}
function savefailCause(status){
    debugger;
    var failCause=$("#failCause1").val();
    updateStatusDone(rechargeId,status,failCause, 0)
}

function updateStatus(status){
    if(status==3){
        $('#failCauseModal').modal();
    }else if(status==2){
        var realAmount = $("#realAmount").val();
        updateStatusDone(rechargeId,status,"",realAmount)
    }
}
function copyTxId(rowId){
    var rowData = jQuery("#jqGrid").jqGrid("getRowData",rowId);
    var tx = rowData.txId;

    let oInput = document.createElement('input');
    oInput.value = tx;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象;
    document.execCommand("Copy"); // 执行浏览器复制命令
    oInput.remove();
    alert(oInput.value)
}

function updateStatusDone(orderId,value,failCause,realAmount){
    url = baseURL + "sys/recharge/updateStatus";
    var updateModel = {id:orderId,
                        status:value,
                        reason:failCause,
                        realAmount:realAmount};
    console.log(updateModel)
    confirm('确定要继续操作？', function(){
        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/json",
            data: JSON.stringify(updateModel),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else{
                    console.log(JSON.stringify(updateModel))
                    alert(r.msg);
                }
            }
        });
    });

}
// function showImg(val,title){
//     $("#showImg").attr("src",val);
//     layer.open({
//         type: 1,
//         skin: 'layui-layer-molv',
//         title: title,
//         area: ['650px', '500px'],
//         shade: 0,
//         shadeClose: false,
//         content: jQuery("#Img"),
//         btn: ['确定'],
//         btn1: function (index) {
//             layer.close(index);
//         }
//     });
// }
function showImg(str) {
    var rowData = jQuery("#jqGrid").jqGrid("getRowData",str);
    var newWin = window.open();
    newWin.document.write("<img height='auto' width='80%' src="+ rowData.evidence +" />")
}
function payTypeSwitch(value){
    if(value == 1){
        return  '<span >微信支付</span>';
    }else if(value == 2){
        return  '<span >支付宝支付</span>';
    }else if(value == 3){
        return  '<span >银行卡支付</span>';
    }
}

